package net.ibizsys.eclipse.studio.xml;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.ibizsys.eclipse.studio.Activator;
import net.ibizsys.eclipse.studio.data.IBizSystem;
import net.ibizsys.eclipse.studio.util.StringKit;

/**
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizSysProxy
{
	private IBizSystem system;

	private List<IBizProject> projectList = new ArrayList<IBizProject>();

	private String rootPath;
	private String pkgpath;
	private boolean bMaven;
	private String javaPath;
	private String webPath;

	public IBizSysProxy(IBizSystem system)
	{
		this.system = system;

		String pkgcodename = system.getPkgcodename();

		if (!StringKit.IsBlank(pkgcodename))
		{
			this.pkgpath = pkgcodename.replaceAll("[.]", "/");
		} else
		{
			this.pkgpath = "";
		}

		this.rootPath = Activator.getDefault().getRootPath() + File.separator + system.getCodename();

		File file = new File(rootPath + File.separator + "pom.xml");

		this.bMaven = file.exists();

		if (bMaven)
		{
			this.javaPath = String.format("%1$s/%2$s/src/main/java/%3$s", Activator.getDefault().getRootPath(), system.getCodename(), getPkgPath());
			this.webPath = String.format("%1$s/%2$s/src/main/webapp", Activator.getDefault().getRootPath(), system.getCodename());
		} else
		{
			this.javaPath = String.format("%1$s/%2$s/src/%3$s", Activator.getDefault().getRootPath(), system.getCodename(), getPkgPath());
			this.webPath = String.format("%1$s/%2$s/WebContent", Activator.getDefault().getRootPath(), system.getCodename());
		}
	}

	public void addProject(IBizProject project)
	{
		projectList.add(project);
	}

	public void addProjects(List<IBizProject> projects)
	{
		projectList.addAll(projects);
	}

	public IBizSystem getSystem()
	{
		return system;
	}

	public List<IBizProject> getProjectList()
	{
		return projectList;
	}

	public String getName()
	{
		if (system != null)
		{
			return system.getCodename();
		}

		return "";
	}

	public String getId()
	{
		if (system != null)
		{
			return system.getId();
		}

		return "";
	}

	public String getSFID()
	{
		if (system != null)
		{
			return system.getSFID();
		}

		return "";
	}

	/**
	 * 获取项目根节点
	 * 
	 * @return
	 */
	public String getRootPath()
	{
		return this.rootPath;
	}

	/**
	 * 获取java代码根包
	 * 
	 * @return
	 */
	public String getJavaRootPath()
	{
		return this.javaPath;
	}

	/**
	 * 获取Web目录绝对路径
	 * 
	 * @return
	 */
	public String getWebRootPath()
	{
		return this.webPath;
	}

	/**
	 * 是否Maven构建项目
	 * 
	 * @return
	 */
	public boolean isMaven()
	{
		return bMaven;
	}

	/**
	 * 包相对路径
	 * 
	 * @return
	 */
	public String getPkgPath()
	{
		return pkgpath;
	}
}
